package com.zm.inlet.service.impl;

import com.zm.api.openFeign.OrderFeign;
import com.zm.api.openFeign.PayFeign;
import com.zm.api.openFeign.ProductFeign;
import com.zm.inlet.service.InletService;
import com.zm.inlet.service.InletTCCService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@GlobalTransactional
@Service
public class InletTCCServiceImpl implements InletTCCService {
    @Resource
    private OrderFeign orderFeign;
    @Resource
    private ProductFeign productFeign;
    @Resource
    private PayFeign payFeign;
    @Override
    public void buyTCC() {
        Long orderId = orderFeign.OrderTCC(1L, 1L); //返回订单号
        if (orderId==0L){
            throw new RuntimeException("TCC订单服务调用失败,事务回滚!");
        }
        boolean pay = payFeign.payTCC(1L, 100L);
        if (!pay){
            throw new RuntimeException("TCC支付失败,事务回滚!");
        }
        Boolean b = productFeign.reduceStockTCC(1L);
        if (!b){
            throw new RuntimeException("TCC库存服务调用失败,事务回滚!");
        }
    }
}
